﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using DevExpress.XtraGrid.Views.Grid;
using System.Data.SqlClient;

namespace COMOSOFT.Production.Forms
{
    public partial class FrmProductionPlanning : Form
    {
        ClassShare cls = new ClassShare("FrmProductionPlanning");
        int Year;
        int SubID;
        object Month,OID,SubIID,SubFLID;
        public FrmProductionPlanning(int _Year,object _Month,object _OID,object _SubIID,object _SubFLID)
        {
            InitializeComponent();
            this.Year = _Year;
            this.Month = _Month;
            this.OID = _OID;
            this.SubIID = _SubIID;
            this.SubFLID = _SubFLID;
        }
        public void LoadData()
        {
            sp_ProductionPlanningTableAdapter.Fill(this.dataSet_Production.sp_ProductionPlanning,Year,Month,OID,SubIID,SubFLID);
            //dockPanel_Line.Close();
        }

        private void bnt_OK_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = cls.connection();
                for(int i=0;i<gridView_Line.RowCount-1;i++)
                {
                    GridView view = bandedGridView_ProductionPlanning as GridView;
                    string sql="insert into ProductionOutlinePlanning ([Year],[Month],OID,SubIID,SubFLID,Line,Quantity) "+
                                "values (@Year,@Month,@OID,@SubIID,@SubFLID,@Line,@Quantity)";                   
                    SqlCommand com = new SqlCommand(sql, con);
                    com.Parameters.AddWithValue("@Year",Convert.ToInt32(view.GetFocusedDataRow()["Year"]));
                    com.Parameters.AddWithValue("@Month",Convert.ToInt32(view.GetFocusedDataRow()["Month"]));
                    com.Parameters.AddWithValue("@OID",Convert.ToInt32(view.GetFocusedDataRow()["OID"]));
                    com.Parameters.AddWithValue("@SubIID",Convert.ToInt32(view.GetFocusedDataRow()["SubIID"]));
                    com.Parameters.AddWithValue("@SubFLID",Convert.ToInt32(view.GetFocusedDataRow()["SubFLID"]));
                    com.Parameters.AddWithValue("@Line", Convert.ToInt32(gridView_Line.GetDataRow(i)["Line"]));                 
                    com.Parameters.AddWithValue("@Quantity", Convert.ToInt32(gridView_Line.GetDataRow(i)["Quantity"]));
                    com.ExecuteNonQuery();
                }
                con.Close();
                LoadData();
                dockPanel_Line.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        } 

        private void addLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                GridView view=bandedGridView_ProductionPlanning as GridView;
                SubID=Convert.ToInt32(view.GetFocusedDataRow()["SubFLID"]);
                factoryLineTableAdapter.Fill(dataSet_Production.FactoryLine, SubID);
                dockPanel_Line.Show();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void repositoryItemLookUpEdit_Line_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.ToString() == "Add")
            {
                dockPanel_AddLine.Show();
            }
        }

        private void bntAdd_Click(object sender, EventArgs e)
        {
            if (txtLine.EditValue != null)
            {
                string strsql = "insert into FactoryLine (SubFLID,Line) values (@SubFLID,@Line)";
                cls.UpdateDataTypeText(strsql, new string[] { "@SubFLID", "@Line" }, new object[] { SubFLID, Convert.ToInt32(txtLine.EditValue) });
                factoryLineTableAdapter.Fill(dataSet_Production.FactoryLine, SubID);               
                gridView_Line.SetFocusedRowCellValue("Line",Convert.ToInt32(txtLine.EditValue));
                txtLine.EditValue = null;
                dockPanel_AddLine.Close();
            }
            else
            {
                MessageBox.Show("Line not null! Please check again!");
            }
        }
    }
}
